package com.zx.flow.core.execute.eventhandler;


import com.zx.flow.api.dict.FlowDict.Event;
import com.zx.flow.api.execute.model.FlowContext;
import com.zx.flow.api.execute.model.FlowNode;
import com.zx.flow.api.execute.request.FlowParamAction;
import com.zx.flow.api.execute.request.FlowParamSkip;
import com.zx.flow.api.execute.response.FlowResult;
import com.zx.flow.api.util.AssertUtil;
import com.zx.flow.core.execute.EventHandler;

/**
 * 事件处理器【流程->跳过】
 */
public class EventFlowSkipHandler extends EventHandler<FlowParamSkip> {

    @Override
    public boolean in(FlowContext flow, FlowParamSkip param) {
        AssertUtil.checkNotSystem(param.getAccountCode());
        return flow != null;
    }

    @Override
    public FlowResult on(FlowContext flow, FlowParamSkip param) {
        // 获得当前节点
        FlowNode currentNode = AssertUtil.checkCurrentNodeId(flow, param.getNodeId());
        // 本质是由系统触发通过action
        flow = this.execute(
                new FlowParamAction(param.getId(), param.getNodeId(), Event.nodeApproved.getCode(), param.getRemark(),
                        param.getAccountCode())).getFlowContext();
        // 触发监听
        this.listener(currentNode, Event.nodeSkip);
        return FlowResult.ok(flow);
    }
}
